package com.tang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.dao.UserDao;
import com.tang.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户业务逻辑
 *
 * @author cfpu003
 */
@Service
public class UserService {
    // private static final Logger logger = Logger
    // .getLogger(AdvertisementManager.class);

    @Resource
    private UserDao userDao;

    /**
     * 获取用户列表
     *
     * @param page
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String, Object> getUserList(Integer page, Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<User> userList = userDao.listBy(null);
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        Map<String, Object> map = new HashMap<>();
        map.put("list", userList);
        map.put("total", pageInfo.getTotal());
        return map;
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = {Exception.class, SQLException.class}, timeout = 60)
    public String addUser(User user) throws Exception {
        userDao.insert(user);
        return user.getUserId();
    }

}
